package com.hoshiicloud.goods.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.goods.entity.goods.GoodsSpecificationsValuesType;
import com.hoshiicloud.goods.feign.IdGeneratorService;
import com.hoshiicloud.goods.mapper.goods.GoodsSpecificationsValuesTypeMapper;
import com.hoshiicloud.goods.service.GoodsSpecificationsValuesTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 规格值分类 服务实现类
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Service
public class GoodsSpecificationsValuesTypeServiceImpl extends ServiceImpl<GoodsSpecificationsValuesTypeMapper, GoodsSpecificationsValuesType> implements GoodsSpecificationsValuesTypeService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    @Override
    @Logable(authName = "商品管理-规格值分类管理-分页查询规格值分类")
    @Cacheable(cacheNames = "GoodsSpecificationsValuesType", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    public Page<GoodsSpecificationsValuesType> getPage(Page<GoodsSpecificationsValuesType> page, Wrapper<GoodsSpecificationsValuesType> queryWrapper, String key) {
        baseMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    @Cacheable(cacheNames = "GoodsSpecificationsValuesType", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "商品管理-规格值分类-查询规格值分类 ")
    public GoodsSpecificationsValuesType getById(Long id) {
        return baseMapper.selectById(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "GoodsSpecificationsValuesType", allEntries = true)
    @Logable(authName = "商品管理-规格值分类-新增规格值分类")
    public boolean save(GoodsSpecificationsValuesType entity) {

        QueryWrapper<GoodsSpecificationsValuesType> q = new QueryWrapper<>();
        q.lambda()
                .eq(GoodsSpecificationsValuesType::getCompanyId,entity.getCompanyId())
                .eq(GoodsSpecificationsValuesType::getType,entity.getType())
                .eq(GoodsSpecificationsValuesType::getName,entity.getName())
                .eq(GoodsSpecificationsValuesType::getDeleted,0);
        GoodsSpecificationsValuesType old = this.getOne(q);
        if(null!= old){
            throw new ServiceException("该租户下已存在规格类型为【"+entity.getType()+"】的规格值分类【"+entity.getName()+"】");
        }

        // 生成主键id
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("生成主键id失败"));
        }

        // 新增数据
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        entity.setDeleted(0);
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "GoodsSpecificationsValuesType", allEntries = true)
    @Logable(authName = "商品管理-规格值分类管理-修改规格值类别")
    public boolean updateById(GoodsSpecificationsValuesType entity) {

        QueryWrapper<GoodsSpecificationsValuesType> q = new QueryWrapper<>();
        q.lambda()
                .eq(GoodsSpecificationsValuesType::getCompanyId,entity.getCompanyId())
                .eq(GoodsSpecificationsValuesType::getType,entity.getType())
                .eq(GoodsSpecificationsValuesType::getName,entity.getName())
                .ne(GoodsSpecificationsValuesType::getId,entity.getId())
                .eq(GoodsSpecificationsValuesType::getDeleted,0);
        GoodsSpecificationsValuesType old = this.getOne(q);
        if(null != old){
            throw new ServiceException("该租户下已存在规格类型为【"+entity.getType()+"】的规格值分类【"+entity.getName()+"】");
        }

        // 修改数据
        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }




    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "GoodsSpecificationsValuesType", allEntries = true)
    @Logable(authName = "商品管理-规格值分类管理-删除规格值分类")
    public boolean deleteById(Long id) {
        // 根据id查询数据
        GoodsSpecificationsValuesType entity = this.getById(id);
        if (entity == null) {
            throw new ServiceException("删除规格值分类异常，无法找到规格值分类记录");
        }

        //进行删除操作
        entity.setUpdatedTime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }

    @Override
    @Cacheable(cacheNames = "GoodsSpecificationsValuesType", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "商品管理-规格值分类管理-根据id集合查询规格值分类")
    public List<GoodsSpecificationsValuesType> getByIds(List<Long> ids, String key) {
        return baseMapper.selectBatchIds(ids);
    }

    @Override
    @Cacheable(cacheNames = "GoodsSpecificationsValuesType", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "商品管理-规格值分类管理-根据租户Id 查询规格值分类下拉列表")
    public List<GoodsSpecificationsValuesType> queryDropdownBox(Long companyId, String type, String key){
        QueryWrapper<GoodsSpecificationsValuesType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(GoodsSpecificationsValuesType::getCompanyId, companyId)
                .eq(GoodsSpecificationsValuesType::getType,type)
                .eq(GoodsSpecificationsValuesType::getDeleted,0)
                //只查询 id 和name 2个字段
                .select(GoodsSpecificationsValuesType::getId,GoodsSpecificationsValuesType::getName);

        List<GoodsSpecificationsValuesType> list = this.list(queryWrapper);
        if(CollectionUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        return list;
    }
}
